package com.kakavr.upload.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.kakavr.base.controller.BaseController;
import com.kakavr.result.MsgResult;
import com.kakavr.result.Result;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.AttachUtils;

/**
 * 文件上传控制器
 * 
 * @author gang.yang
 */
@RestController
@RequestMapping("attach")
public class AttachController extends BaseController {

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public void uploadFile(
			@RequestParam("file") MultipartFile file,
			HttpServletRequest request, HttpServletResponse response) {
		if (null == file || file.isEmpty()) {
			Result.outJson(
					Result.errorResult(MsgConstants.MSG_CODE_1001, "上传文件不能为空"),
					response);
			return;
		}
		String loadUrl = AttachUtils.uploadFile(file);
		if ("error".equals(loadUrl)) {
			Result.outJson(Result.errorResult(MsgConstants.MSG_CODE_1001,
					"文件类型不支持或者上传出错"), response);
			return;
		}
		info("上传后文件路径："+loadUrl);
		Result.outJson(MsgResult.okResultData(loadUrl), response);
	}

}
